package elastic.util;

import com.alibaba.fastjson.JSONObject;
import com.css.sword.knowing.search.Searcher;
import css.sword.util.HttpClientUtil;
import org.apache.cxf.common.util.StringUtils;
import org.apache.http.ParseException;
import org.apache.tika.Tika;
import org.apache.tika.exception.TikaException;

import java.io.*;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;

public class ExtensionTool {

	public String extractString(File file) throws IOException, TikaException {
		Tika tika = new Tika();

		FileInputStream inputstream = new FileInputStream(file);

		return tika.parseToString(inputstream);
	}

	public String extractString(String content) throws IOException, TikaException {
		Tika tika = new Tika();

		InputStream is = new ByteArrayInputStream(content.getBytes());

		return tika.parseToString(is);
	}
	
	public String fetchContext(String indexName, String pkVal, String keyName)
	{
		try {
			Map<String, Object> map = Searcher.fetchDoc(indexName, "all", pkVal);
			if(null != map)
			{
				Object val = map.get(keyName);
				
				if(null != val)
				{
					return String.valueOf(val);
				}
			}
				
		} catch (IOException e) {
			return null;
		}
		
		return null;
	}
	
	public String fetchContextFromEs(String url, String fieldName)
    {
        try {
            String ret = httpDownload(url);
            
            if(!StringUtils.isEmpty(ret))
            {
                JSONObject json = (JSONObject)JSONObject.parse(ret);
                
                if(json.getBoolean("found"))
                {
                    JSONObject source = json.getJSONObject("_source");
                    
                    if(source != null)
                        return source.getString(fieldName);
                }
            }
                
        } catch (Exception e) {
            return null;
        }
        
        return null;
    }
	
	public String httpDownload(String url) {
	
		try {
			HttpClientUtil httpDownload = HttpClientUtil.getInstance();
			return httpDownload.httpGet(url, null);
		} catch (ParseException e) {
		    System.out.println(e.getMessage());
		}
		return null;
	}

	public String httpExtract(String url, String name, int maxLen) {
		String ret = "";
        String fname = "";

		String userDirh = System.getProperties().getProperty("user.dir"); 	

		if(!StringUtils.isEmpty(name))
			fname = name + String.valueOf(Thread.currentThread().getId()) + ".html";
		else
			fname = String.valueOf(Thread.currentThread().getId()) + ".html";
		Path p = Paths.get(userDirh, fname);

		File f = new File(p.toString());
		if(f.exists())
			f.delete();

		HttpClientUtil httpDownload = HttpClientUtil.getInstance();
		httpDownload.httpDownloadFile(url+name, p.toString(), null);

		if(f.exists())
		{
			if(maxLen <= 0)
			    maxLen = 1024*1024;
			
			try {
			    if(f.length() <= 60*maxLen)
			    {
		            Tika tika = new Tika();
	                tika.setMaxStringLength(maxLen);
			        ret = tika.parseToString(f);
			    }
			} catch (IOException e) {
			} catch (TikaException e) {
			}
			
			f.delete();
		}

		return ret;
	}
	
	public String ftpDownload(String path) {
		return "";
	}

	public static void main(String[] args) {
		ExtensionTool e = new ExtensionTool();
		
		long tm0 = System.currentTimeMillis();
		//	String ret = e.httpExtract("http://hdfgj.bjhd.gov.cn/xxgk/xxgs/zdly/zbgs/201807/P020180726389654449974.docx");
		//txt
//	    String ret = e.httpExtract("https://csfile.szoa.sz.gov.cn/file/download?md5Path=33de571876d02243df6290e12f9b0074@10437", "name", 0);
		//doc
//		String ret = e.httpExtract("http://csfile.szoa.sz.gov.cn/file/download?md5Path=b08447225910415c86ded88fae4c3ad4@29184", "name", 0);
		//zip
//		String ret = e.httpExtract("http://csfile.szoa.sz.gov.cn/file/download?md5Path=cd975012330e921c29eb23f1eb3f09de@18296185", "name", 0);
		//jpg
//		String ret = e.httpExtract("http://csfile.szoa.sz.gov.cn/file/download?md5Path=08a7760466af74d08aaa843c4db6c441@115601", "name", 0);
		//png
//		String ret = e.httpExtract("http://csfile.szoa.sz.gov.cn/file/download?md5Path=09da65b8b2e7b6befdff4e40c267e218@103049", "name", 0);
		//rar
//		String ret = e.httpExtract("http://csfile.szoa.sz.gov.cn/file/download?md5Path=b56fc3ac6c0e832f5c1f9bf00749ae76@53978", "name", 0);
		//pdf
//		String ret = e.httpExtract("http://csfile.szoa.sz.gov.cn/file/download?md5Path=6025f3e914f11f36fa3316eb36e25afa@1113818", "name", 0);
		//xlsx
		//String ret = e.httpExtract("http://csfile.szoa.sz.gov.cn/file/download?md5Path=9a97fbe6b04f0100327a6ec0a1dafada@32848", "name", 0);
//
		//String ret  = e.fetchContextFromEs("http://admin:szoa%402018@10.248.86.9:9001/files_cache/all/" + "34fe2ab3dadc6ccef03ee39b3e864b62@195120", "context");
      
		long tm1 = System.currentTimeMillis();
			System.out.println("timet1: " + (tm1 - tm0));
			
		//	ret = e.httpExtract("http://hdfgj.bjhd.gov.cn/xxgk/xxgs/zdly/zbgs/201807/P020180726389654449974.docx");
			
		//	ret = e.httpExtract("https://csfile.szoa.sz.gov.cn/file/download?md5Path=f533dbd9d67794ea79108593d609a9f9@58458");
			
			long tm2 = System.currentTimeMillis();
		System.out.println("timet2: " + (tm2 - tm1));
		//System.out.println("ret: " + ret);
	}
}
